package engine.managers {

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;

	/**
	 * Manager class to access embeded assets (Images, Music, etc..)
	 * @author Jon Smelquist
	 */
	public class AssetManager {

		private static var _instance:AssetManager;
		private static var _allowInstantiation:Boolean = false;
		private var _assetList:Dictionary;
		
		/*
		 * Assets
		 */
		[Embed(source='/assets/guard.png')]
		public static const GUARD:Class;
		[Embed(source='/assets/yheti.png')]
		public static const YHETI:Class;
		[Embed(source='/assets/tavern.png')]
		public static const TAVERN:Class;
		[Embed(source='/assets/chips.cave.blue.png')]
		public static const CAVE_BLUE:Class;
		[Embed(source='/assets/outdoors.png')]
		public static const OUTDOORS:Class;
		
		
		/**
		 * Method to return the AssetManager instance if one exists,
		 * and if it does not exist we create one.
		 * @return _instance AssetManager object instance.
		 */
		public static function getInstance():AssetManager {
			if (_instance == null) {
				_allowInstantiation = true;
				_instance = new AssetManager();
				_allowInstantiation = false;
				_instance.buildAssetList();
			}
			return _instance;
		}
		
		public function AssetManager() {
			if (!_allowInstantiation)
				throw new Error("This class cannot be instantiated! Use getInstance() instead.");
			
		}
		// Function for grabbing an asset based on its string representation
		public function getAsset(assetName:String):BitmapData {
			return _assetList[ assetName ];
		}
		
		private function buildAssetList():void
		{
			_assetList = new Dictionary();
			
			_assetList[ "chips.guard" ] = makeBitmapData(GUARD);
			_assetList[ "chips.cave.blue" ] =  makeBitmapData(CAVE_BLUE);
			_assetList[ "chips.yheti" ] =  makeBitmapData(YHETI);
			_assetList[ "outdoors" ] =  makeBitmapData(OUTDOORS);
		}
		
		private function makeBitmapData(imageSource:Class):BitmapData
		{
			var source:Bitmap = new imageSource();
			var myBitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0x00000000 );
			myBitmapData.draw( source );
			return myBitmapData;
		}
		
	}
}
